﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDClient.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Management;
using System.Net;
using System.Net.Sockets;
using System.Text;

namespace PSDClient.Service
{
    public static class DeviceHelper
    {
        public static dl_download_device DeviceInfo { get; set; }
        public static List<HardDiskPartition> HardDiskList { get; set; }

        static DeviceHelper()
        {
            string uuid = DeviceUUID.GetUUID();
            DeviceInfo = GetDeviceRegistInfo(uuid);

            string ip = GetLocalIP();
            GetLocalStorage();
        }

        /// <summary>
        /// 注册当前下载设备
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        private static dl_download_device GetDeviceRegistInfo(string id)
        {
            string response = HttpHelper.Get(Settings.API.GetDownloadDeviceInfo + "?deviceId=" + id);

            var result = JsonConvert.DeserializeObject<HResult<dl_download_device>>(response);

            if (result.success && result.data != null)
            {
                return result.data;
            }
            else
            {
                var device = new dl_download_device();
                device.id = id;
                device.device_ip = GetLocalIP();
                device.device_name = "下载设备_" + device.device_ip;
                device.device_connect_type = Settings.DeviceConnectType;
                device.data_path = Settings.DefaultDataPath;
                device.state = 1;

                FrmRegist frmRegist = new FrmRegist();
                frmRegist.DeviceInfo = device;
                frmRegist.ShowDialog();

                return device;
            }
        }

        /// <summary>
        /// 获取当前设备IP
        /// </summary>
        /// <returns></returns>
        public static string GetLocalIP()
        {
            string ip = "";
            try
            {
                IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
                foreach (IPAddress item in IpEntry.AddressList)
                {
                    if (item.AddressFamily == AddressFamily.InterNetwork)
                    {
                        ip = item.ToString();
                        break;
                    }
                }

            }
            catch { }

            return ip;
        }

        /// <summary>
        /// 获取当前设备存储信息
        /// </summary>
        public static void GetLocalStorage()
        {
            HardDiskList = new List<HardDiskPartition>();

            try
            {
                SelectQuery selectQuery = new SelectQuery("select * from win32_logicaldisk");

                ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery);

                ManagementObjectCollection diskcollection = searcher.Get();
                if (diskcollection != null && diskcollection.Count > 0)
                {
                    HardDiskPartition harddisk = null;
                    foreach (ManagementObject disk in searcher.Get())
                    {
                        int nType = Convert.ToInt32(disk["DriveType"]);
                        if (nType != Convert.ToInt32(DriveType.Fixed))
                        {
                            continue;
                        }
                        else
                        {
                            harddisk = new HardDiskPartition();
                            harddisk.FreeSpace = Convert.ToDouble(disk["FreeSpace"]) / (1024 * 1024 * 1024);
                            harddisk.SumSpace = Convert.ToDouble(disk["Size"]) / (1024 * 1024 * 1024);
                            harddisk.PartitionName = disk["DeviceID"].ToString();
                            HardDiskList.Add(harddisk);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

    }

    /// 
    /// 盘符信息
    /// 
    public class HardDiskPartition
    {
        #region Data
        private string _PartitionName;
        private double _FreeSpace;
        private double _SumSpace;
        #endregion //Data

        #region Properties
        /// 
        /// 空余大小
        /// 
        public double FreeSpace
        {
            get { return _FreeSpace; }
            set { this._FreeSpace = value; }
        }
        /// 
        /// 使用空间
        /// 
        public double UseSpace
        {
            get { return _SumSpace - _FreeSpace; }
        }
        /// 
        /// 总空间
        /// 
        public double SumSpace
        {
            get { return _SumSpace; }
            set { this._SumSpace = value; }
        }
        /// 
        /// 分区名称
        /// 
        public string PartitionName
        {
            get { return _PartitionName; }
            set { this._PartitionName = value; }
        }
        /// 
        /// 是否主分区
        /// 
        public bool IsPrimary
        {
            get
            {
                //判断是否为系统安装分区
                if (System.Environment.GetEnvironmentVariable("windir").Remove(2) == this._PartitionName)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        #endregion //Properties
    }
}
